package cn.linkai.hadoop.storm;

import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseBasicBolt;
import org.apache.storm.tuple.Tuple;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

public class SuffixBolt extends BaseBasicBolt {

    FileWriter writer=null;

    private static final String suffix=" is ok";

    @Override
    public void prepare(Map stormConf, TopologyContext context) {

        try {
            writer = new FileWriter("/home/hadoop/stormoutput/"+ UUID.randomUUID());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
    }

    @Override
    public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
        String upper=tuple.getString(0);

        try {
            writer.write(upper+suffix+"\n");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
